package com.example.dem.ouser.rest;


import cn.hutool.http.HttpUtil;
import com.example.demo.commons.entity.Student;
import com.example.demo.commons.result.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author root
 * @date 2021-01-25 11:19
 */
@RestController
@Slf4j
public class HelloRest {

    @Autowired
    RestTemplate restTemplate;

    private final static String URL = "http://localhost:1002/test";
    private final static String URL_1 = "http://localhost:1002/test?name={name}";

    private final static String URL_S = "http://localhost:1002/getStudent";


    @GetMapping("test")
    public String test() {

        Map<String, Object> map = new HashMap<>();
        map.put("name", "zhangsan");
        //        HttpUtil.post(url, );
        String s = HttpUtil.get(URL, map);
        return s;
    }

    @GetMapping("restTest1")
    public String restTest() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "zhangSan");

        String forObject = restTemplate.getForObject(URL_1, String.class, map);
        log.info(" object {}", forObject);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(URL_1, String.class, map);
        log.info(" entity {}", forEntity);

        ResponseEntity<String> forEntity1 = restTemplate.getForEntity(URL_1, String.class, "1234");
        log.info(" entity1 {}", forEntity1);

        return forEntity1.getBody();
    }


    @GetMapping("student")
    public ResultVo<Student> get() {
        ResponseEntity<Student> forEntity = restTemplate.getForEntity(URL_S, Student.class);
        log.info("forEntity.getForEntity.Student  {}", forEntity);
        log.info("body   {}", forEntity.getBody());

        ResponseEntity<ResultVo> forEntity1 = restTemplate.getForEntity(URL_S, ResultVo.class);
        log.info("forEntity.getForEntity.ResultVo  {}", forEntity1);
        log.info("body1   {}", forEntity1.getBody());


        Student forEntity2 = restTemplate.getForObject(URL_S, Student.class);
        log.info("forEntity.getForObject.Student  {}", forEntity2);


        ResultVo<Student> forEntity3 = restTemplate.getForObject(URL_S, ResultVo.class);
        log.info("forEntity.getForObject.ResultVo  {}", forEntity3);
        return forEntity3;
    }
}
